//
//  GFClassifyViewController.m
//  GoodFood
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFClassifyViewController.h"

@interface GFClassifyViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flow;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSDictionary *mainDic;
@property (nonatomic, strong) NSMutableArray *array;
@end

@implementation GFClassifyViewController

- (void)viewWillAppear:(BOOL)animated {
    
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:10001];
    view.backgroundColor = [UIColor colorWithRed:190 / 255.0 green:231 / 255.0 blue:233 / 255.0 alpha:1];
    
    
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:209/255.0 green:186/255.0 blue:116/255.0 alpha:1];
    
#pragma mark - 修改navi颜色
    /* 修改navigation title 颜色 */
    
    UIColor *color = [UIColor colorWithRed:209/255.0 green:186/255.0 blue:116/255.0 alpha:1];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];/* 在NSFontAttributeName中寻找*/
    
    self.navigationController.navigationBar.titleTextAttributes = dic;
    

}

#pragma mark _ 处理数据
- (void)handleData {
    
    NSString *frontUrlStr = @"http://food.boohee.com/fb/v1/categories?kind=";
    
    NSString *urlStr = [frontUrlStr stringByAppendingString:self.needUrlStr];
    
    [AFNetHandle GETWithURL:urlStr body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        self.mainDic = [NSDictionary dictionaryWithDictionary:result];
        
        self.array = [NSMutableArray array];
        
        NSArray *arr = [self.mainDic objectForKey:@"categories"];
        
        for (NSDictionary *dic in arr) {
            
            GFClassifyModel *classModel = [GFClassifyModel modWithDic:dic];
            
            [self.array addObject:classModel];
            
        }
        
        //        NSLog(@"%@", self.array);
        
        if ([self.needUrlStr isEqualToString:@"season"]) {
            
            [self.tableView reloadData];
            
        } else {
            
            [self.collectionView reloadData];
            
        }
        [self.overTimeView removeFromSuperview];
        [self.waitView removeFromSuperview];
        
    } fail:^(NSError *error) {
        [self.overTimeView removeFromSuperview];
        self.overTimeView = [[GFOverTimeView alloc] initWithFrame:self.view.frame withButtonBlock:^(UIButton *button) {
            [self.view addSubview:self.waitView];
            [self handleData];
        }];
        [self.waitView removeFromSuperview];
        [self.view addSubview:self.overTimeView];
    }];
    
    
}

#pragma mark _ 创建collectionView
- (void)creatCollectionView {
    
    self.flow = [[UICollectionViewFlowLayout alloc] init];
    self.flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.flow.minimumInteritemSpacing = ScreenWidth / 375 * 10;
    self.flow.minimumLineSpacing = ScreenWidth / 375 * 10;
    if ([self.needUrlStr isEqualToString:@"cooking"]) {
        self.flow.itemSize = CGSizeMake(ScreenWidth / 375 * 111, ScreenWidth / 375 * 50);
    } else {
        self.flow.itemSize = CGSizeMake(ScreenWidth / 375 * 111, ScreenWidth / 375 * 111);
    }
    self.flow.sectionInset = UIEdgeInsetsMake(ScreenWidth / 375 * 10, ScreenWidth / 375 * 10, ScreenWidth / 375 * 10, ScreenWidth / 375 * 10);
    
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) collectionViewLayout:self.flow];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.backgroundColor = [UIColor colorWithRed:237 / 255.0 green:237 / 255.0 blue:230 / 255.0 alpha:1];
    
    
    /* 重用池 */
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.collectionView registerClass:[LabelWithPicCell class] forCellWithReuseIdentifier:@"LabelWithPicCell"];
    [self.collectionView registerClass:[LabelCell class] forCellWithReuseIdentifier:@"LabelCell"];
    
    
    [self.view addSubview:self.collectionView];

}


#pragma mark _ collectionDataSource的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.needUrlStr isEqualToString:@"cooking"]) {
        
        LabelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LabelCell" forIndexPath:indexPath];
        
        cell.classModel = [self.array objectAtIndex:indexPath.item];
        
        return cell;
        
    } else {
        
        LabelWithPicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LabelWithPicCell" forIndexPath:indexPath];
        
        cell.classModel = [self.array objectAtIndex:indexPath.item];
        
        return cell;
    }
}

#pragma mark _ collection item点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GFPandectViewController *panVC = [[GFPandectViewController alloc] init];
    
    panVC.kindName = [self.mainDic objectForKey:@"kind"];
    
    NSString *value = [NSString stringWithFormat:@"&value=%ld&order_by=", indexPath.item + 1];
    NSString *useValue = [NSString stringWithFormat:@"%ld", indexPath.item + 1];
    
    GFClassifyModel *classModel = [self.array objectAtIndex:indexPath.item];
    
    panVC.titleName = classModel.name;
    
    panVC.value = value;
    panVC.useValue = useValue;
    
    //    NSLog(@"%@%@", [self.mainDic objectForKey:@"kind"], value);
    
    [self.navigationController pushViewController:panVC animated:YES];
    
}



#pragma mark _ 创建tableView
- (void)creatTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = ScreenWidth / 375 * 70;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    /* 重用池 */
    [self.tableView registerClass:[SeasonCell class] forCellReuseIdentifier:@"SeasonCell"];
    
    [self.view addSubview:self.tableView];

    
}

#pragma mark _ tableView DataSource 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.array.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SeasonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SeasonCell"];
    
    GFClassifyModel *classModel = [self.array objectAtIndex:indexPath.row];
    
    cell.rowNumber = indexPath.row;
    cell.classModel = classModel;
    
    return cell;
}

#pragma mark _ tableView row点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GFPandectViewController *panVC = [[GFPandectViewController alloc] init];
    
    panVC.kindName = [self.mainDic objectForKey:@"kind"];
    
    NSString *value = [NSString stringWithFormat:@"&value=%ld&order_by=", indexPath.row + 1];
    NSString *useValue = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    
    GFClassifyModel *classModel = [self.array objectAtIndex:indexPath.row];
    
    panVC.titleName = classModel.name;
    
    panVC.value = value;
    panVC.useValue = useValue;
    
    //    NSLog(@"%@%@", [self.mainDic objectForKey:@"kind"], value);
    
    [self.navigationController pushViewController:panVC animated:YES];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.titleName;
    
    if ([self.needUrlStr isEqualToString:@"season"]) {
        
        [self creatTableView];
        
    } else {
        
        [self creatCollectionView];
        
    }
    
    [self.view bringSubviewToFront:self.waitView];
    
    [self handleData];
    
    
    // Do any additional setup after loading the view.
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"houtui1"] style:UIBarButtonItemStylePlain target:self action:@selector(backToLastVC:)];
    self.navigationItem.leftBarButtonItem = backItem;

}

- (void)backToLastVC:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
    
    YRSideViewController *con = (YRSideViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    
    [con showLeftViewController:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
